package com.hyk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.domain.Notice;
import com.hyk.dto.R;
import com.hyk.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Arrays;

@RestController
@Api(tags = "公告管理")
@RequestMapping("/notices")
public class NoticController {

    @Resource
    private NoticeService noticeService;

    /**
     * 分页查询公告
     */
    @GetMapping
    @ApiOperation("分页查询公告列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title",value = "标题"),
            @ApiImplicitParam(name = "startTime",value = "开始时间"),
            @ApiImplicitParam(name = "endTime",value = "结束时间"),
            @ApiImplicitParam(name = "status",value = "状态"),
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "每页的大小")
    })
    @PreAuthorize("hasAuthority('notice_query')")
    public R<Page<Notice>> findByPage(@ApiIgnore Page<Notice> page,
                                      String title,
                                      String startTime,
                                      String endTime,
                                      Integer status){
        return R.ok(noticeService.findByPage(page,title,startTime,endTime,status));
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除一个公告")
    @PreAuthorize("hasAuthority('notice_delete')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "要删除的id的集合")
    })
    public R delete(@RequestBody String[] ids) {
        if (ids == null || ids.length == 0){
            return R.fail("请选择要删除的公告!");
        }
        boolean b = noticeService.removeByIds(Arrays.asList(ids));
        if (b){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "启用/禁用一个公告")
    @PreAuthorize("hasAuthority('notice_update')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要启用/禁用一个公告的公告Id"),
            @ApiImplicitParam(name = "status", value = "要设置的公告状态")

    })
    public R updateStatus(Long id,Integer status){
        Notice notice = new Notice();
        notice.setId(id);
        notice.setStatus(status);
        boolean update = noticeService.updateById(notice);
        if (update){
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }

    @PatchMapping
    @ApiOperation(value = "修改一个公告")
    @PreAuthorize("hasAuthority('notice_update')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "作者"),
            @ApiImplicitParam(name = "content", value = "内容"),
            @ApiImplicitParam(name = "created",value = "创建时间"),
            @ApiImplicitParam(name = "description",value = "简介"),
            @ApiImplicitParam(name = "id",value = "id"),
            @ApiImplicitParam(name = "lastUpdateTime",value = "更新时间"),
            @ApiImplicitParam(name = "sort",value = "文章排序，越大越靠前"),
            @ApiImplicitParam(name = "status",value = "状态"),
            @ApiImplicitParam(name = "title",value = "标题")

    })
    public R updateStatus(@RequestBody @Validated Notice notice){
        boolean update = noticeService.updateById(notice);
        if (update){
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }

    @PostMapping
    @ApiOperation("新增加一个公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "author", value = "作者"),
            @ApiImplicitParam(name = "content", value = "内容"),
            @ApiImplicitParam(name = "created",value = "创建时间"),
            @ApiImplicitParam(name = "description",value = "简介"),
            @ApiImplicitParam(name = "id",value = "id"),
            @ApiImplicitParam(name = "lastUpdateTime",value = "更新时间"),
            @ApiImplicitParam(name = "sort",value = "文章排序，越大越靠前"),
            @ApiImplicitParam(name = "status",value = "状态"),
            @ApiImplicitParam(name = "title",value = "标题")
    })
    @PreAuthorize("hasAuthority('notice_create')")
    public R add(@RequestBody @Validated Notice notice){
        notice.setStatus(1);
        boolean save = noticeService.save(notice);
        if (save){
            return R.ok("新增成功");
        }
        return R.fail("新增失败");
    }

    @GetMapping("/simple")
    public R<Page<Notice>> simple(Page<Notice> page){
        Page<Notice> page1 = noticeService.page(page, new LambdaQueryWrapper<Notice>().
                eq(Notice::getStatus, 1).
                orderByDesc(Notice::getSort));
        return R.ok(page1);
    }

    @GetMapping("/simple/{id}")
    public R<Notice> simpleId(@PathVariable(value = "id") Long id){
        Notice notice = noticeService.getById(id);
        return R.ok(notice);
    }

}
